package com.workpaper.app.plan.view;


import java.util.Calendar;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.consts.plan.DailyPlanViewConsts;
import com.workpaper.app.plan.entity.DailyPlan;

public class DailyPlanInfoDialog extends Dialog{
	/**
	 * 日期选择器
	 */
	private DateTime planDateSelector;
	/**
	 * 内容文本
	 */
	private StyledText contentText;
	/**
	 * 执行人
	 */
	private Text executorText; 
	/**
	 * 备注
	 */
	private StyledText remarkText;
	
	/**
	 * 要展示的数据
	 */
	private DailyPlan dailyPlan;
	
	public DailyPlanInfoDialog(Shell parentShell, DailyPlan dailyPlan) {
		super(parentShell);
		this.dailyPlan = dailyPlan;
	}
	
	/**
	 * 配置窗口标题、图标等信息
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanInfoViewTitle));	//标题
//		newShell.setImage(null);	//图标
	}

	

	
	@Override
	protected Point getInitialSize() {
		return new Point(400, 400);
	}
	

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 6;
		container.setLayout(layout);
		
		//创建用日期：标签、日期控件 输入框
		Label dsNameLabel = new Label(container, SWT.NONE);
		dsNameLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Date));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		dsNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		dsNameLabel.setImage(CacheImage.getAppImage(ImagePath.LOGIN_USERNAME_ICO));
		planDateSelector = new DateTime(container, SWT.DATE);
		planDateSelector.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		planDateSelector.setEnabled(false);
		//创建内容：
		Label contentLabel = new Label(container, SWT.NONE);
		contentLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Content));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		contentLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		contentText = new StyledText(container, SWT.BORDER);
		contentText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		contentText.setEnabled(false);
		//创建执行人：
		Label executorLabel = new Label(container, SWT.NONE);
		executorLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Executor));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		executorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		executorText = new Text(container, SWT.BORDER);
		executorText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		executorText.setEnabled(false);
		//创建备注：
		Label remarkLabel = new Label(container, SWT.NONE);
		remarkLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Remark));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		remarkLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		remarkText = new StyledText(container, SWT.BORDER);
		remarkText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		remarkText.setEnabled(false);
		/*
		 * 展示数据，必须要在控件初始化之后才能填充数据
		 */
		showData();
		return container;
	}


	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.CANCEL_ID, ViewInfoCache.get(CommonViewConsts.ButtonText_Return), true);
	}
	
	private void showData() {
		if(dailyPlan == null) {
			return;
		}
		Calendar planDate = Calendar.getInstance();
		planDate.setTime(dailyPlan.getPlanDate());
		planDateSelector.setYear(planDate.get(Calendar.YEAR));
		planDateSelector.setMonth(planDate.get(Calendar.MONTH));
		planDateSelector.setDay(planDate.get(Calendar.DAY_OF_MONTH));
		
		contentText.setText(dailyPlan.getContent());
		executorText.setText(dailyPlan.getExecutor());
		remarkText.setText(dailyPlan.getRemark());
	}
	
}